﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;

/*
 *  ContentPresenter o = this.ItemContainerGenerator.ContainerFromIndex(i) as ContentPresenter;
 *  ItemsControl child = o.ContentTemplate.FindName("PART_ChildItemsControl", o) as ItemsControl;
 */

namespace DemoReception
{
    public class CRoomItemsControl : ItemsControl
    {
        private ScrollViewer iScrollViewer;

        private List<CHotelRoomCatalog> iDefaultItemsSourceLocal;

        public List<CHotelRoomCatalog> iDefaultItemsSource
        {
            get { return iDefaultItemsSourceLocal; }
            set
            {
                this.iDefaultItemsSourceLocal = value;
                base.ItemsSource = value;
            }
        }

        static CRoomItemsControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CRoomItemsControl), new FrameworkPropertyMetadata(typeof(CRoomItemsControl)));
        }

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            iScrollViewer = this.Template.FindName("PART_ParentScrollViewer", this) as ScrollViewer;

        }

    }

    public class CRoomItem : Control
    {
        private static readonly Typeface iTypeface = new Typeface("Microsoft YaHei");
        

        static CRoomItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(CRoomItem), new FrameworkPropertyMetadata(typeof(CRoomItem)));
        }

        public CRoomItem()
        {

        }

        public void RenderOverride()
        {
            this.InvalidateVisual();
        }

        protected override void OnRender(DrawingContext drawingContext)
        {
            base.OnRender(drawingContext);

            CRoomStat contextNode = this.DataContext as CRoomStat;
            if (contextNode != null)
            {
                BitmapImage image = null;
                GuidelineSet guideLines = new GuidelineSet();
                
                guideLines.GuidelinesX.Add(1.5);
                guideLines.GuidelinesY.Add(1.5);
                drawingContext.PushGuidelineSet(guideLines);
                drawingContext.DrawRectangle(CRoomControlHelper.GetBackgroundBrush(contextNode), CRoomControlHelper.GetBorderPen(contextNode), new Rect(new Point(1, 1), new Point(this.RenderSize.Width - 1, this.RenderSize.Height - 1)));
                drawingContext.Pop();
                drawingContext.DrawText(new FormattedText(contextNode.iLocation, System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, iTypeface, 12, CRoomControlHelper.KTextForeground), new Point(2, 0));

                double offset = 0;
                image = null;
                switch (contextNode.iGuestStatCode)
                {
                    case CGuestStatusCode.KToLeave: image = CRoomControlHelper.KToLeaveImage; break;
                    case CGuestStatusCode.KToArrive: image = CRoomControlHelper.KToArriveImage; break;
                }
                if (image != null)
                {
                    drawingContext.DrawImage(image, new Rect(new Point(offset + 2, (int)(this.RenderSize.Height / 2)), new Size(image.PixelWidth, image.PixelHeight)));
                }
                  
            }
        }

    }

    public static class CRoomControlHelper
    {
        public static readonly Pen KRedPen = new Pen(Brushes.Red, 1);
        public static readonly Pen KTransparentPen = new Pen(Brushes.Transparent, 1);
        public static readonly Pen KWhitePen = new Pen(Brushes.White, 1);

        public static readonly SolidColorBrush KTextForeground = new SolidColorBrush(Color.FromRgb(0x66, 0x66, 0x66));

        public static readonly SolidColorBrush KEmptyCleanBrush = new SolidColorBrush(Color.FromRgb(0xcc, 0xed, 0xc2));
        public static readonly SolidColorBrush KEmptyDirtyBrush = new SolidColorBrush(Color.FromRgb(0x99, 0xcc, 0x8a));
        public static readonly SolidColorBrush KSettleCleanBrush = new SolidColorBrush(Color.FromRgb(0xff, 0xdc, 0xdc));
        public static readonly SolidColorBrush KSettleDirtyBrush = new SolidColorBrush(Color.FromRgb(0xff, 0xa6, 0xa6));
        public static readonly SolidColorBrush KExceptBrush = new SolidColorBrush(Color.FromRgb(0xf1, 0xf1, 0xf1));

        public static readonly Pen KEmptyCleanPen = new Pen(KEmptyCleanBrush, 1);
        public static readonly Pen KEmptyDirtyPen = new Pen(KEmptyDirtyBrush, 1);
        public static readonly Pen KSettleCleanPen = new Pen(KSettleCleanBrush, 1);
        public static readonly Pen KSettleDirtyPen = new Pen(KSettleDirtyBrush, 1);
        public static readonly Pen KExceptPen = new Pen(KExceptBrush, 1);


        public static readonly BitmapImage KToLeaveImage = new BitmapImage(new Uri("pack://application:,,,/Images/ToLeave.png", UriKind.Absolute));
        public static readonly BitmapImage KToArriveImage = new BitmapImage(new Uri("pack://application:,,,/Images/ToArrive.png", UriKind.Absolute));
        public static readonly BitmapImage KToLeaveAndArriveImage = new BitmapImage(new Uri("pack://application:,,,/Images/ToLeave.png", UriKind.Absolute));

        public static readonly BitmapImage KNoRoomStatImage = new BitmapImage(new Uri("pack://application:,,,/Images/NoRoomStat.png", UriKind.Absolute));


        static CRoomControlHelper()
        {
            KRedPen.Freeze();
            KTransparentPen.Freeze();
            KWhitePen.Freeze();

            KTextForeground.Freeze();

            KEmptyCleanBrush.Freeze();
            KEmptyDirtyBrush.Freeze();
            KSettleCleanBrush.Freeze();
            KSettleDirtyBrush.Freeze();
            KExceptBrush.Freeze();

            KEmptyCleanPen.Freeze();
            KEmptyDirtyPen.Freeze();
            KSettleCleanPen.Freeze();
            KSettleDirtyPen.Freeze();
            KExceptPen.Freeze();

            KToLeaveImage.Freeze();
            KToArriveImage.Freeze();
            KToLeaveAndArriveImage.Freeze();
            KNoRoomStatImage.Freeze();
        }

        public static Brush GetBackgroundBrush(CRoomStat aNode)
        {
            switch (aNode.iRoomStatCode)
            {
                case CRoomStatusCode.KEmptyClean: return KEmptyCleanBrush;
                case CRoomStatusCode.KEmptyDirty: return KEmptyDirtyBrush;
                case CRoomStatusCode.KSettleClean: return KSettleCleanBrush;
                case CRoomStatusCode.KSettleDirty: return KSettleDirtyBrush;
                case CRoomStatusCode.KExcept: return KExceptBrush;
            }
            return Brushes.White;
        }

        public static Pen GetBorderPen(CRoomStat aNode)
        {
            switch (aNode.iRoomStatCode)
            {
                case CRoomStatusCode.KEmptyClean: return KEmptyCleanPen;
                case CRoomStatusCode.KEmptyDirty: return KEmptyDirtyPen;
                case CRoomStatusCode.KSettleClean: return KSettleCleanPen;
                case CRoomStatusCode.KSettleDirty: return KSettleDirtyPen;
                case CRoomStatusCode.KExcept: return KExceptPen;
            }
            return KWhitePen;
        }
    }

    public static class CVisualTreeHelper
    {
      
        public static T GetParent<T>(DependencyObject aDpObj) where T : DependencyObject
        {
            DependencyObject parentObj = aDpObj;
            while (parentObj != null && parentObj.GetType() != typeof(T))
            {
                if (parentObj is Visual || parentObj is Visual3D)
                {
                    parentObj = VisualTreeHelper.GetParent(parentObj);
                }
                else
                {
                    parentObj = LogicalTreeHelper.GetParent(parentObj);
                }
            }
            return parentObj as T;
        }
        
    }
}
